package com.example.springboot.oss.service;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.*;
import com.qcloud.cos.exception.CosClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

@Service
public class CosService {

    private final COSClient cosClient;

    @Autowired
    public CosService(COSClient cosClient) {
        this.cosClient = cosClient;
    }

    public void uploadObject(String bucketName, String key, String filePath) {
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, new File(filePath));
            cosClient.putObject(putObjectRequest);
            System.out.println("Upload Object Success!");
        } catch (CosClientException e) {
            e.printStackTrace();
        }
    }

    public void deleteObject(String bucketName, String key) {
        try {
            DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucketName, key);
            cosClient.deleteObject(deleteObjectRequest);
            System.out.println("Delete Object Success!");
        } catch (CosClientException e) {
            e.printStackTrace();
        }
    }

    public List<COSObjectSummary> listObjects(String bucketName) {
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
        listObjectsRequest.setBucketName(bucketName);
        try {
            ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
            return objectListing.getObjectSummaries();
        } catch (CosClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void updateObject(String bucketName, String key, String newFilePath) {
        deleteObject(bucketName, key);
        uploadObject(bucketName, key, newFilePath);
    }

    public void getObject(String bucketName, String key, String downloadPath) {
        try {
            // 创建 GetObjectRequest 对象
            GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
            // 获取对象
            COSObject cosObject = cosClient.getObject(getObjectRequest);

            // 创建本地文件输出流
            File downloadFile = new File(downloadPath);
            FileOutputStream fileOutputStream = new FileOutputStream(downloadFile);

            // 将 COS 对象的内容保存到文件中
            InputStream objectContent = cosObject.getObjectContent();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = objectContent.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }
            // 关闭流
            fileOutputStream.close();
            objectContent.close();
            System.out.println("Download Object Success! File saved to " + downloadPath);
        } catch (CosClientException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public URL generatePresignedUrl(String bucketName, String key) {
        try {
            GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key);
            // 设置URL过期时间为1小时
            generatePresignedUrlRequest.setExpiration(java.util.Date.from(java.time.Instant.now().plusSeconds(3600)));
            return cosClient.generatePresignedUrl(generatePresignedUrlRequest);
        } catch (CosClientException e) {
            e.printStackTrace();
            return null;
        }
    }
}